//
//  SEGMissionRecordDetailViewController.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/8/19.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGMissionRecordDetailViewController.h"
#import "SEGMissionRecordDetailNormalCell.h"
#import "SEGMissionRecordDetailTopInfoCell.h"
#import "SEGMissionRecordOffLineCodeCell.h"
#import "SEGShippingAddressTableViewCell.h"
#import "SEGConfirmAlertView.h"
#import "SEGMissionRecordDetailViewModel.h"


@interface SEGMissionRecordDetailViewController ()<UITableViewDelegate, UITableViewDataSource,SEGMissionRecordDetailTopInfoCellDelegate,SEGConfirmAlertViewDelegate>
//,SEGAdressListViewControllerDelegate

/**
 主视图
 */
@property (nonatomic, strong)UITableView *tableView;


/*
 确认框
 */
@property (nonatomic, strong) SEGConfirmAlertView *confirmView;

@property (nonatomic, strong) SEGMissionRecordDetailViewModel *viewModel;

@end

@implementation SEGMissionRecordDetailViewController
- (instancetype)initWithWinnerId:(NSString *)winnerId {
    if (self = [super init]) {
        self.viewModel = [[SEGMissionRecordDetailViewModel alloc] initWithWinnerId:winnerId];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"记录信息";
    [self seg_loadRecordDetail];
}

- (void)seg_loadRecordDetail {
    [[self.viewModel.recordDetailCommand execute:@""] subscribeNext:^(NSNumber *x) {
        if (x.boolValue) {
            [SVProgressHUD show];
        }else {
            [SVProgressHUD dismiss];
        }
    } error:^(NSError * _Nullable error) {
        [SVProgressHUD dismiss];
        [SEGAlertView showTmepMessageWithMessage:error.localizedDescription];
    } completed:^{
        [SVProgressHUD dismiss];
        [self.tableView reloadData];
    }];
}

#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    switch (self.viewModel.prizeType) {
        case SEGWinReordPrizeType_PhoneBill://话费
        {
            return 2;
        }
        case SEGWinReordPrizeType_Coin://积分
        {
            return 2;
        }
        case SEGWinReordPrizeType_RechargeCard://充值卡
        {
            if ([[SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data.winnerStatus isEqualToString:@"2"]) {//已发放
                return 3;
            }else {
                return 2;
            }
        }
        case SEGWinReordPrizeType_OnLine://线上实物
        {
            return 2;
        }
        case SEGWinReordPrizeType_OffLine://线下实物
        {
            return 1;
        }
        case SEGWinReordPrizeType_Discount://优惠券
        {
            return 2;
        }
    }
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (self.viewModel.prizeType) {
        case SEGWinReordPrizeType_PhoneBill: {
            //话费
            switch (section) {
                case 0:
                    return 1;
                default:
                    return [self recordDetailNormalCellTypes].count;
            }
        }
        case SEGWinReordPrizeType_Coin: {
            //积分
            switch (section) {
                case 0:
                    return 1;
                default:
                    return [self recordDetailNormalCellTypes].count;
            }
        }
        case SEGWinReordPrizeType_RechargeCard: {
            //充值卡
            if ([[SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data.winnerStatus isEqualToString:@"2"]) {//已发放
                switch (section) {
                    case 0:
                        return 1;
                    case 1://卡号密码信息
                        return 2;
                    default:
                        return [self recordDetailNormalCellTypes].count;
                }
            } else {
                switch (section) {
                    case 0:
                        return 1;
                    default:
                        return [self recordDetailNormalCellTypes].count;
                }
            }
        }
        case SEGWinReordPrizeType_OnLine: {
            //线上实物
            switch (section) {
                case 0:
                    return 1;
                default:
                     return [self recordDetailNormalCellTypes].count;
            }
        }
        case SEGWinReordPrizeType_OffLine: {
            //线下实物
            return [self recordDetailNormalCellTypes].count + 1;
        }
        case SEGWinReordPrizeType_Discount: {
            //优惠券
            //线上实物
            switch (section) {
                case 0:
                    return 1;
                default:
                     return [self recordDetailNormalCellTypes].count;
            }
        }
    }
    return 0;
}

- (NSArray *)recordDetailNormalCellTypes {
    NSMutableArray *displayTypes = [@[@(SEGMissionRecordDetailNormalCellType_Prize),
                                      @(SEGMissionRecordDetailNormalCellType_Act),
                                      @(SEGMissionRecordDetailNormalCellType_Time),
                                      @(SEGMissionRecordDetailNormalCellType_InvaildTime),
                                      @(SEGMissionRecordDetailNormalCellType_Status)] mutableCopy];
    BOOL needShowInvaildTime = YES;
    switch (self.viewModel.prizeType) {
        case SEGWinReordPrizeType_PhoneBill:
            needShowInvaildTime = YES;
            break;
        case SEGWinReordPrizeType_Coin:
            break;
        case SEGWinReordPrizeType_RechargeCard:
            break;
        case SEGWinReordPrizeType_OnLine: {
            needShowInvaildTime = YES;
            // 有收货地址时显示地址信息显示快递信息
            if ([SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data.addressee.length > 0) {
                [displayTypes addObject:@(SEGMissionRecordDetailNormalCellType_ExpressInfo)];
            }
        }
            break;
        case SEGWinReordPrizeType_OffLine:
            needShowInvaildTime = YES;
            break;
        case SEGWinReordPrizeType_Discount:
            needShowInvaildTime = YES;
            break;
    }
    // 判断奖品失效时间是否有效
    if (needShowInvaildTime && !([SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data.prizeTime.floatValue > 0)) {
        needShowInvaildTime = NO;
    }
    if (!needShowInvaildTime) {
        [displayTypes removeObjectAtIndex:3];
    }
    return [displayTypes copy];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger section = indexPath.section;
    switch (self.viewModel.prizeType) {
        case SEGWinReordPrizeType_PhoneBill: {
            //话费
            switch (section) {
                case 0: {
                    SEGMissionRecordDetailTopInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:kWinRecordDetailTopInfoCellIdentifier forIndexPath:indexPath];
                    cell.delegate = self;
                    
                    [cell setUpCellWithModel:[SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data];
                    return cell;
                }
                default: {
                    NSInteger typeIndex = [[[self recordDetailNormalCellTypes] objectAtIndex:indexPath.row] integerValue];
                    SEGMissionRecordDetailNormalCell *cell = [tableView dequeueReusableCellWithIdentifier:kWinRecordDetailNormalIdentifier forIndexPath:indexPath];
                    [cell setUpCellWithModel:[SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data type:typeIndex];
                    return cell;
                }
            }
        }
        case SEGWinReordPrizeType_Coin: {
            //积分
            switch (section) {
                case 0: {
                    SEGMissionRecordDetailTopInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:kWinRecordDetailTopInfoCellIdentifier forIndexPath:indexPath];
                    [cell setUpCellWithModel:[SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data];
                    return cell;
                }
                default: {
                    NSInteger typeIndex = [[[self recordDetailNormalCellTypes] objectAtIndex:indexPath.row] integerValue];
                    SEGMissionRecordDetailNormalCell *cell = [tableView dequeueReusableCellWithIdentifier:kWinRecordDetailNormalIdentifier forIndexPath:indexPath];
                    [cell setUpCellWithModel:[SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data type: typeIndex];
                    return cell;
                }
            }
        }
        case SEGWinReordPrizeType_RechargeCard: {
            //充值卡
            if ([[SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data.winnerStatus isEqualToString:@"2"]) {//已发放
                switch (section) {
                    case 0: {
                        SEGMissionRecordDetailTopInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:kWinRecordDetailTopInfoCellIdentifier forIndexPath:indexPath];
                        [cell setUpCellWithModel:[SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data];
                        return cell;
                    }
                    case 1: {
                        //卡号密码信息
                        SEGMissionRecordDetailNormalCell *cell = [tableView dequeueReusableCellWithIdentifier:kWinRecordDetailNormalIdentifier forIndexPath:indexPath];
                        switch (indexPath.row) {
                            case 0: {
                                [cell setUpCellWithModel:[SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data type:SEGMissionRecordDetailNormalCellType_CardNo];
                                break;
                            }
                            default: {
                                [cell setUpCellWithModel:[SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data type:SEGMissionRecordDetailNormalCellType_Password];
                                break;
                            }
                        }
                        return cell;
                    }
                    default: {
                        NSInteger typeIndex = [[[self recordDetailNormalCellTypes] objectAtIndex:indexPath.row] integerValue];
                        SEGMissionRecordDetailNormalCell *cell = [tableView dequeueReusableCellWithIdentifier:kWinRecordDetailNormalIdentifier forIndexPath:indexPath];
                        [cell setUpCellWithModel:[SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data type:typeIndex];
                        return cell;
                    }
                }
            } else {
                switch (section) {
                    case 0: {
                        SEGMissionRecordDetailTopInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:kWinRecordDetailTopInfoCellIdentifier forIndexPath:indexPath];
                        [cell setUpCellWithModel:[SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data];
                        return cell;
                    }
                    default: {
                        NSInteger typeIndex = [[[self recordDetailNormalCellTypes] objectAtIndex:indexPath.row] integerValue];
                        SEGMissionRecordDetailNormalCell *cell = [tableView dequeueReusableCellWithIdentifier:kWinRecordDetailNormalIdentifier forIndexPath:indexPath];
                        [cell setUpCellWithModel:[SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data type: typeIndex];
                        return cell;
                    }
                }
            }
        }
        case SEGWinReordPrizeType_OnLine: {
            //线上实物
            switch (section) {
                case 0: {
                    if ([SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data.addressee.length > 0) {
                        //有收货地址时显示地址信息
                        SEGShippingAddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddresscellId" forIndexPath:indexPath];
                        cell.arrowImg.hidden = YES;
                        [cell showWithModel:self.viewModel.addressData];
                        return cell;
                    } else {
                        SEGMissionRecordDetailTopInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:kWinRecordDetailTopInfoCellIdentifier forIndexPath:indexPath];
                        cell.delegate = self;
                        [cell setUpCellWithModel:[SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data];
                        return cell;
                    }
                }
                default: {
                    NSInteger typeIndex = [[[self recordDetailNormalCellTypes] objectAtIndex:indexPath.row] integerValue];
                    SEGMissionRecordDetailNormalCell *cell = [tableView dequeueReusableCellWithIdentifier:kWinRecordDetailNormalIdentifier forIndexPath:indexPath];
                    [cell setUpCellWithModel:[SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data type:typeIndex];
                    return cell;
                }
            }
        }
        case SEGWinReordPrizeType_OffLine: {
            //线下实物
            switch (indexPath.row) {
                case 0: {
                    SEGMissionRecordOffLineCodeCell *cell = [tableView dequeueReusableCellWithIdentifier:kWinRecordDetailOffLineCodeCellIdentifier forIndexPath:indexPath];
                    [cell setUpCellWithAcceptNum:[SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data.redemptionCode];
                    if ([[SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data.winnerStatus isEqualToString:@"3"]) {
                        [cell showInvailState];
                    }
                    return cell;
                }
                default:  {
                    NSInteger typeIndex = [[[self recordDetailNormalCellTypes] objectAtIndex:indexPath.row - 1] integerValue];
                    SEGMissionRecordDetailNormalCell *cell = [tableView dequeueReusableCellWithIdentifier:kWinRecordDetailNormalIdentifier forIndexPath:indexPath];
                    [cell setUpCellWithModel:[SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data type:typeIndex];
                    return cell;
                }
            }
        }
        case SEGWinReordPrizeType_Discount: {
            //优惠券
            switch (section) {
                case 0: {
                    SEGMissionRecordDetailTopInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:kWinRecordDetailTopInfoCellIdentifier forIndexPath:indexPath];
                    cell.delegate = self;
                    [cell setUpCellWithModel:[SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data];
                    return cell;
                }
                default: {
                    NSInteger typeIndex = [[[self recordDetailNormalCellTypes] objectAtIndex:indexPath.row] integerValue];
                    SEGMissionRecordDetailNormalCell *cell = [tableView dequeueReusableCellWithIdentifier:kWinRecordDetailNormalIdentifier forIndexPath:indexPath];
                    [cell setUpCellWithModel:[SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data type:typeIndex];
                    return cell;
                }
            }
        }
    }
    return [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView*)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    return 44;
}
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.01;
    } else {
        return 10;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth(), 10.0)];
    header.backgroundColor = kColor_bg;
    return header;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - SEGMissionRecordDetailTopInfoCellDelegate
- (void)submitRechangePhone:(NSString *)phoneNumber {
    self.confirmView = [[SEGConfirmAlertView alloc] init];
    [self.confirmView showAlertWithType:SEGConfirmAlertViewType_Phone contentText:phoneNumber delegate:self];
}

- (void)getPrizeAction {
    
    
    if ([[SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data.prizeType integerValue] == SEGWinReordPrizeType_Discount) {
        // 优惠券
        NSString *link = [SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data.links;
        if (link && link.length > 0) {
            //NSDictionary *params = @{@"linkURL" : link};
            //UIViewController *vc = [[SEGUhomeMediator sharedInstance] seg_initWebViewLoadController:params];
            //[self.navigationController pushViewController:vc animated:YES];
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString:link]];
        }
    }else{
        // 线上实物
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:[self.viewModel getPrizeActionParams]];
        [dic setValue:self forKey:@"delegate"];
        
        UIViewController *vc = [[SEGUhomeMediator sharedInstance] seg_initSEGAdressListViewController:dic];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

//选择了一个配送地址
- (void)responseAddressWidhData:(SEGAddressModel*)data {
    self.viewModel.addressData = data;
    // 更新奖品纪录模型信息
    
    [SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data.address = self.viewModel.addressData.userAddress;
    [SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data.addressee = self.viewModel.addressData.contactName;
    [SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data.wTel = self.viewModel.addressData.contactPhone;
    [self.tableView reloadData];
}

#pragma mark - SEGConfirmAlertViewDelegate
- (void)alertViewConfirmButtonAction {
    [SVProgressHUD show];
    //提交手机号
    [[self.viewModel.submitRechargeTelCommand execute:self.confirmView.contentText] subscribeError:^(NSError * _Nullable error) {
        [SVProgressHUD dismiss];
        [SEGAlertView showTmepMessageWithMessage:error.localizedDescription];
    } completed:^{
        [SVProgressHUD dismiss];
        [SEGAlertView showTmepMessageWithMessage:@"提交成功"];
        [self seg_loadRecordDetail];
    }];
}

#pragma mark - getter
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth(), LYScreenHeight() - kNavigationBarAndStatusBarHeight) style:UITableViewStylePlain];
        _tableView.backgroundColor = kColor_bg;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorColor = kColor_L2;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        [_tableView registerClass:[SEGMissionRecordDetailTopInfoCell class] forCellReuseIdentifier:kWinRecordDetailTopInfoCellIdentifier];
        [_tableView registerClass:[SEGMissionRecordDetailNormalCell class] forCellReuseIdentifier:kWinRecordDetailNormalIdentifier];
        [_tableView registerClass:[SEGMissionRecordOffLineCodeCell class] forCellReuseIdentifier:kWinRecordDetailOffLineCodeCellIdentifier];
        [_tableView registerClass:[SEGShippingAddressTableViewCell class] forCellReuseIdentifier:@"AddresscellId"];
        _tableView.tableFooterView = [[UIView alloc] init];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}


@end
